/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.codegen.jfinal.creator;

import com.hyts.codegen.common.model.JavaFieldModel;
import com.hyts.codegen.common.model.SimpleBuildModel;
import com.hyts.codegen.jfinal.base.BaseJfinalCreator;
import com.hyts.codegen.jfinal.model.JfinalBuildModel;
import com.hyts.guava.String.Strings;
import com.hyts.guava.object.Objects;
import com.hyts.template.common.BaseJavaFileTemplate;
import com.hyts.template.strformat.StringCodeTemplate;

/** 
 * @title DefaultJfinalCodeCreator.java  
 * @package com.hyts.codegen.jfinal.creator  
 * @description TODO  
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年5月12日 
 * @version V1.0 
 */
public class DefaultJfinalCodeCreator implements BaseJfinalCreator<SimpleBuildModel> {
	
	/**  
	 * <p>{字段的描述}</p> 
	 * @fieldName codeTemplate
	 * @fieldType BaseJavaFileTemplate
	 */ 
	private BaseJavaFileTemplate codeTemplate;
	
	/**  
	 * @constructor：DefaultJfinalCodeCreator  
	 */
	public DefaultJfinalCodeCreator(
			BaseJavaFileTemplate codeTemplate) {
		Objects.checkNull(codeTemplate);
		this.codeTemplate = codeTemplate;
	}
	
	public DefaultJfinalCodeCreator() {
		super();
		this.codeTemplate = new StringCodeTemplate();
	}

	/* (非 Javadoc)
	 * @param model
	 * @return
	 * @see com.hyts.codegen.jfinal.base.BaseJfinalWriter#buildModel(java.lang.Object)  
	 * @exception
	 */ 
	
	@Override
	public String buildModel(SimpleBuildModel model) {
		JfinalBuildModel jfinalModel = new JfinalBuildModel(model).initModel();
		if(Strings.isNotEmpty(codeTemplate.getJavaCode().toString()))
			codeTemplate.flushJavaCode();
		codeTemplate.genPackageContent(jfinalModel.getBasePackageName(),jfinalModel.getModelPackageName());
		codeTemplate.genImportContent(jfinalModel.getImportClasses());
		codeTemplate.genJavaDocContent(jfinalModel.getDocModel().getAnnotationMap(),"");
		codeTemplate.genTypeContent(jfinalModel.getTypeModel().getJavaType(),
									jfinalModel.getTypeModel().getAccessType(),
									jfinalModel.getTypeModel().getClassName(),
									jfinalModel.getTypeModel().getAnnotations());
		codeTemplate.genFieldContent(jfinalModel.getFieldModels().get(0).getAccessType(), 
									 jfinalModel.getFieldModels().get(0).getReturnType(), 
									 jfinalModel.getFieldModels().get(0).getFieldName(), 
									 jfinalModel.getFieldModels().get(0).getHeadSpace(),
									 jfinalModel.getFieldModels().get(0).getAnnotations());
		return codeTemplate.end().toString();
	}

	/* (非 Javadoc)
	 * <b>构建JFINAL框架基础结构代码-  buildDao </b>
	 * @param model
	 * @return
	 * @see com.hyts.codegen.jfinal.base.BaseJfinalWriter#buildDao(java.lang.Object)  
	 * @exception
	 */ 
	
	@Override
	public String buildDao(SimpleBuildModel model) {
		JfinalBuildModel jfinalModel = new JfinalBuildModel(model).initDao();
		if(Strings.isNotEmpty(codeTemplate.getJavaCode().toString()))
		codeTemplate.flushJavaCode();
		codeTemplate.genPackageContent(jfinalModel.getBasePackageName(),jfinalModel.getModelPackageName());
		codeTemplate.genImportContent(jfinalModel.getImportClasses());
		codeTemplate.genJavaDocContent(jfinalModel.getDocModel().getAnnotationMap(),"");
		codeTemplate.genTypeContent(jfinalModel.getTypeModel().getJavaType(),
									jfinalModel.getTypeModel().getAccessType(),
									jfinalModel.getTypeModel().getClassName(),
									jfinalModel.getTypeModel().getAnnotations());
		for(int i = 0 ; i< jfinalModel.getMethodModels().size() ; i++){
			codeTemplate.genMethodContent(jfinalModel.getMethodModels().get(i).getAccessType(),
					jfinalModel.getMethodModels().get(i).getReturnType(), 
					jfinalModel.getMethodModels().get(i).getMethodName(),
					jfinalModel.getMethodModels().get(i).getParamList(), 
					jfinalModel.getMethodModels().get(i).getFunctionBody(), 
					jfinalModel.getMethodModels().get(i).getHeadSpace(),
					jfinalModel.getMethodModels().get(i).getAnnotations());
		}
		return codeTemplate.end().toString();
	}

	/*
	 * @param model
	 * @return
	 * @see com.hyts.codegen.jfinal.base.BaseJfinalWriter#buildService(java.lang.Object)  
	 * @exception
	 */ 
	
	@Override
	public String buildService(SimpleBuildModel model) {
		JfinalBuildModel jfinalModel = new JfinalBuildModel(model).initService(model.isInterface());
		if(Strings.isNotEmpty(codeTemplate.getJavaCode().toString()))
			codeTemplate.flushJavaCode();
		codeTemplate.genPackageContent(jfinalModel.getBasePackageName(),jfinalModel.getModelPackageName());
		codeTemplate.genImportContent(jfinalModel.getImportClasses());
		codeTemplate.genJavaDocContent(jfinalModel.getDocModel().getAnnotationMap(),"");
		codeTemplate.genTypeContent(jfinalModel.getTypeModel().getJavaType(),
				jfinalModel.getTypeModel().getAccessType(),
				jfinalModel.getTypeModel().getClassName(),
				jfinalModel.getTypeModel().getAnnotations());
		if(jfinalModel.getFieldModels() != null && jfinalModel.getFieldModels().size() > 0) {
			for (int j = 0; j < jfinalModel.getFieldModels().size(); j++) {
				JavaFieldModel field = jfinalModel.getFieldModels().get(j);
				codeTemplate.genFieldContent(field.getAccessType(), field.getReturnType(), field.getFieldName(), field.getHeadSpace(), field.getAnnotations());
			}
		}
		for(int i = 0 ; i< jfinalModel.getMethodModels().size() ; i++){
			codeTemplate.genMethodContent(jfinalModel.getMethodModels().get(i).getAccessType(),
					jfinalModel.getMethodModels().get(i).getReturnType(),
					jfinalModel.getMethodModels().get(i).getMethodName(),
					jfinalModel.getMethodModels().get(i).getParamList(),
					jfinalModel.getMethodModels().get(i).getFunctionBody(),
					jfinalModel.getMethodModels().get(i).getHeadSpace(),model.isInterface(),
					jfinalModel.getMethodModels().get(i).getAnnotations());
		}
		return codeTemplate.end().toString();
	}

	/* (非 Javadoc)
	 * @param model
	 * @return
	 * @see com.hyts.codegen.jfinal.base.BaseJfinalWriter#buildController(java.lang.Object)  
	 * @exception
	 */ 
	
	@Override
	public String buildController(SimpleBuildModel model) {
		JfinalBuildModel jfinalModel = new JfinalBuildModel(model).initController();
		if(Strings.isNotEmpty(codeTemplate.getJavaCode().toString()))
			codeTemplate.flushJavaCode();
		codeTemplate.genPackageContent(jfinalModel.getBasePackageName(),jfinalModel.getModelPackageName());
		codeTemplate.genImportContent(jfinalModel.getImportClasses());
		codeTemplate.genJavaDocContent(jfinalModel.getDocModel().getAnnotationMap(),"");
		codeTemplate.genTypeContent(jfinalModel.getTypeModel().getJavaType(),
									jfinalModel.getTypeModel().getAccessType(),
									jfinalModel.getTypeModel().getClassName(),
									jfinalModel.getTypeModel().getAnnotations());
		codeTemplate.genFieldContent(jfinalModel.getFieldModels().get(0).getAccessType(), 
									 jfinalModel.getFieldModels().get(0).getReturnType(), 
									 jfinalModel.getFieldModels().get(0).getFieldName(), 
									 jfinalModel.getFieldModels().get(0).getHeadSpace(),
									 jfinalModel.getFieldModels().get(0).getAnnotations());
		for(int i = 0 ; i< jfinalModel.getMethodModels().size() ; i++){
			codeTemplate.genMethodContent(jfinalModel.getMethodModels().get(i).getAccessType(),
					jfinalModel.getMethodModels().get(i).getReturnType(),
					jfinalModel.getMethodModels().get(i).getMethodName(),
					jfinalModel.getMethodModels().get(i).getParamList(),
					jfinalModel.getMethodModels().get(i).getFunctionBody(),
					jfinalModel.getMethodModels().get(i).getHeadSpace(),model.isInterface(),
					jfinalModel.getMethodModels().get(i).getAnnotations());
		}
		return codeTemplate.end().toString();
	}

}
